package com.cbs.android.component.weather.caiyun;

import com.cbs.android.component.network.SimpleResponseHandler;
import com.cbs.android.component.weather.caiyun.entity.CaiyunWeather;
import com.cbs.android.component.weather.weatherinterface.WeatherHandler;
import com.cbs.android.component.weather.weatherinterface.constant.WeatherType;
import com.cbs.android.component.weather.weatherinterface.constant.WindDirection;
import com.cbs.android.component.weather.weatherinterface.constant.WindLevel;
import com.cbs.android.component.weather.weatherinterface.entity.Forecast;
import com.cbs.android.component.weather.weatherinterface.entity.Weather;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by broche on 6/9/16.
 */
public class ForecastResponseHandler extends SimpleResponseHandler<CaiyunWeather> {
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private WeatherHandler weatherHandler;
    private Forecast forecast;

    public ForecastResponseHandler(double lat, double lng, WeatherHandler weatherHandler) {
        forecast = new Forecast().setLatitude(lat).setLongitude(lng);
        this.weatherHandler = weatherHandler;
    }

    public ForecastResponseHandler(String city, int type, WeatherHandler weatherHandler) {
        if (type == 0) {
            forecast = new Forecast().setCityCode(city);
        } else {
            forecast = new Forecast().setCityName(city);
        }
        this.weatherHandler = weatherHandler;
    }

    @Override
    public void onSuccess(CaiyunWeather entity) {
        if (entity.getStatus().equals("ok")) {
            if (entity.getResult() != null) {
                if (entity.getResult().getDaily() != null) {
                    if (entity.getResult().getDaily().getTemperature() != null) {
                        for (CaiyunWeather.ResultEntity.DailyEntity.TemperatureEntity temperatureEntity : entity.getResult().getDaily().getTemperature()) {
                            try {
                                long timestamp = simpleDateFormat.parse(temperatureEntity.getDate()).getTime() / 1000;
                                Weather weather = checkExisted(forecast.getWeathers(), timestamp);
                                if (weather == null) {
                                    weather = new Weather();
                                } else {
                                    forecast.getWeathers().remove(weather);
                                }
                                weather.setTimestamp(timestamp).setTemperatureMax((float) temperatureEntity.getMax())
                                        .setTemperatureMin((float) temperatureEntity.getMin());
                                forecast.getWeathers().add(weather);
                            } catch (ParseException e) {
                                // TODO what to do
                            }
                        }
                    } else {
                        weatherHandler.onError("获取失败, 请稍后再试");
                    }
                    if (entity.getResult().getDaily().getSkycon() != null) {
                        for (CaiyunWeather.ResultEntity.DailyEntity.SkyconEntity skyconEntity : entity.getResult().getDaily().getSkycon()) {
                            try {
                                long timestamp = simpleDateFormat.parse(skyconEntity.getDate()).getTime() / 1000;
                                Weather weather = checkExisted(forecast.getWeathers(), timestamp);
                                if (weather == null) {
                                    weather = new Weather();
                                } else {
                                    forecast.getWeathers().remove(weather);
                                }
                                weather.setTimestamp(timestamp);
                                if (skyconEntity.getValue().equals("CLEAR_DAY")) {
                                    weather.setWeatherType(WeatherType.CLEAR);
                                } else if (skyconEntity.getValue().equals("CLEAR_NIGHT")) {
                                    weather.setWeatherType(WeatherType.CLEAR);
                                } else if (skyconEntity.getValue().equals("PARTLY_CLOUDY_DAY")) {
                                    weather.setWeatherType(WeatherType.PARTLY_CLOUDY);
                                } else if (skyconEntity.getValue().equals("PARTLY_CLOUDY_NIGHT")) {
                                    weather.setWeatherType(WeatherType.PARTLY_CLOUDY);
                                } else if (skyconEntity.getValue().equals("CLOUDY")) {
                                    weather.setWeatherType(WeatherType.CLOUDY);
                                } else if (skyconEntity.getValue().equals("RAIN")) {
                                    weather.setWeatherType(WeatherType.RAIN);
                                } else if (skyconEntity.getValue().equals("SLEET")) {
                                    weather.setWeatherType(WeatherType.RAIN);
                                } else if (skyconEntity.getValue().equals("SNOW")) {
                                    weather.setWeatherType(WeatherType.SNOW);
                                } else if (skyconEntity.getValue().equals("WIND")) {
                                    weather.setWeatherType(WeatherType.WIND);
                                } else if (skyconEntity.getValue().equals("FOG")) {
                                    weather.setWeatherType(WeatherType.FOG);
                                } else if (skyconEntity.getValue().equals("HAZE")) {
                                    weather.setWeatherType(WeatherType.HAZE);
                                } else {
                                    weather.setWeatherType(WeatherType.CLEAR);
                                }

                                forecast.getWeathers().add(weather);
                            } catch (ParseException e) {
                                // TODO what to do
                            }
                        }
                    } else {
                        weatherHandler.onError("获取失败, 请稍后再试");
                    }
                    if (entity.getResult().getDaily().getAqi() != null) {
                        for (CaiyunWeather.ResultEntity.DailyEntity.AqiEntity aqiEntity : entity.getResult().getDaily().getAqi()) {
                            try {
                                long timestamp = simpleDateFormat.parse(aqiEntity.getDate()).getTime() / 1000;
                                Weather weather = checkExisted(forecast.getWeathers(), timestamp);
                                if (weather == null) {
                                    weather = new Weather();
                                } else {
                                    forecast.getWeathers().remove(weather);
                                }
                                weather.setTimestamp(timestamp).setAqi((float) aqiEntity.getAvg());
                                forecast.getWeathers().add(weather);
                            } catch (ParseException e) {
                                // TODO what to do
                            }
                        }
                    } else {
                        weatherHandler.onError("获取失败, 请稍后再试");
                    }
                    if (entity.getResult().getDaily().getPm25() != null) {
                        for (CaiyunWeather.ResultEntity.DailyEntity.Pm25Entity pm25Entity : entity.getResult().getDaily().getPm25()) {
                            try {
                                long timestamp = simpleDateFormat.parse(pm25Entity.getDate()).getTime() / 1000;
                                Weather weather = checkExisted(forecast.getWeathers(), timestamp);
                                if (weather == null) {
                                    weather = new Weather();
                                } else {
                                    forecast.getWeathers().remove(weather);
                                }
                                weather.setTimestamp(timestamp).setPm25((float) pm25Entity.getAvg());
                                forecast.getWeathers().add(weather);
                            } catch (ParseException e) {
                                // TODO what to do
                            }
                        }
                    } else {
                        weatherHandler.onError("获取失败, 请稍后再试");
                    }
                    if (entity.getResult().getDaily().getPrecipitation() != null) {
                        for (CaiyunWeather.ResultEntity.DailyEntity.PrecipitationEntity precipitationEntity : entity.getResult().getDaily().getPrecipitation()) {
                            try {
                                long timestamp = simpleDateFormat.parse(precipitationEntity.getDate()).getTime() / 1000;
                                Weather weather = checkExisted(forecast.getWeathers(), timestamp);
                                if (weather == null) {
                                    weather = new Weather();
                                } else {
                                    forecast.getWeathers().remove(weather);
                                }
                                weather.setTimestamp(timestamp).setPrecipitation((float) precipitationEntity.getAvg());
                                forecast.getWeathers().add(weather);
                            } catch (ParseException e) {
                                // TODO what to do
                            }
                        }
                    } else {
                        weatherHandler.onError("获取失败, 请稍后再试");
                    }
                    if (entity.getResult().getDaily().getWind() != null) {
                        for (CaiyunWeather.ResultEntity.DailyEntity.WindEntity windEntity : entity.getResult().getDaily().getWind()) {
                            try {
                                long timestamp = simpleDateFormat.parse(windEntity.getDate()).getTime() / 1000;
                                Weather weather = checkExisted(forecast.getWeathers(), timestamp);
                                if (weather == null) {
                                    weather = new Weather();
                                } else {
                                    forecast.getWeathers().remove(weather);
                                }
                                weather.setTimestamp(timestamp).setWindDirection(WindDirection.fromAngle(windEntity.getAvg().getDirection()))
                                        .setWindLevel(WindLevel.fromSpeed(windEntity.getAvg().getSpeed()));
                                forecast.getWeathers().add(weather);
                            } catch (ParseException e) {
                                // TODO what to do
                            }
                        }
                    } else {
                        weatherHandler.onError("获取失败, 请稍后再试");
                    }
                    // success
                    weatherHandler.onForecast(forecast);
                } else {
                    weatherHandler.onError("获取失败, 请稍后再试");
                }
            } else {
                weatherHandler.onError("获取失败, 请稍后再试");
            }
        } else {
            weatherHandler.onError(entity.getStatus());
        }
    }

    @Override
    public void onFailure(int code, String message) {
        weatherHandler.onError("获取失败, 请稍后再试");
    }

    private Weather checkExisted(List<Weather> weathers, long timestamp) {
        for (Weather weather : weathers) {
            if (weather.getTimestamp() == timestamp) {
                return weather;
            }
        }
        return null;
    }
}
